extends Control


var edit_data = null

onready var type_input = $PanelContainer/VBoxContainer/type/SpinBox
onready var coin_num  = $PanelContainer/VBoxContainer/coinNum/SpinBox
onready var skill_op  = $PanelContainer/VBoxContainer/skill/SearchSelect


signal close

func _ready():
	var skill_list = DataSource.get_serie_skill_list()
	skill_op.set_data("选择技能", skill_list) 


func set_edit_data(data):
	self.edit_data = data
	
	if edit_data:
		type_input.value = data["type"]
		coin_num.value = int(data.get("ipCoin",1))
		skill_op.set_value(data["skillId"])



func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()



func _on_ok_button_up():
	if edit_data:
		edit_data["type"] = type_input.value
		edit_data["ipCoin"] = coin_num.value
		edit_data["skillId"] = skill_op.get_value()
		
		DataSource.update_data(DataSource.teach_shop, edit_data)
		MainTool.toast_info("更新成功!")
	else:
		var data = {
			"type": type_input.value,
			"skillId": skill_op.get_value(),
			"ipCoin": coin_num.value
		}
		
		if data["skillId"] < 0:
			MainTool.toast_info("请选择技能!")
			return
		
		var list = DataSource.get_data_list(DataSource.teach_shop)
		for item in list:
			if item["type"] == data["type"] and item["skillId"] == data["skillId"]:
				MainTool.toast_info("不能重复添加!")
				return
				
		DataSource.add_data(DataSource.teach_shop, data)
	
		MainTool.toast_info("添加成功!")
	
	MainTool.getUILayer().pop_ui()
	
	emit_signal("close")
